var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    companyId:0,
    companyName:null,
    rules:[{
          name: 'name',
          rules: [{required: true, message: '企业名称不能为空!'},{minlength:4,message:'企业名称最少为4个汉字!'}],
      }],
    formData:{},
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '变更企业名称', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad(options) {
    var _this = this;
    _this.setData({
      companyId:parseInt(options.companyId),
      companyName:options.companyName
    });
    requestClient = new ZYRequestClient();
  },
  onShow(){
    var userInfo = wx.getStorageSync('userInfo');
    if(userInfo==''){
      wx.showModal({
        title:'请授权登录！',
        content:'您还未授权登录，请在授权登录后再创建企业！',
        showCancel:false,
        success(r){
          wx.reLaunch({
            url: '../../../login/index',
          })
        }
      })
    }
  },
  formInputChange(e) {
      const {field} = e.currentTarget.dataset
      this.setData({
          [`formData.${field}`]: e.detail.value
      })
  },
  submitForm(e){
    var that = this;
    this.selectComponent('#form').validate((valid, errors) => {
        if (!valid) {
            const firstError = Object.keys(errors)
            if (firstError.length) {
                this.setData({
                    error: errors[firstError[0]].message
                })
                wx.showToast({
                  title: errors[firstError[0]].message,
                  icon:'none'
                })
            }
        } else {
            wx.showLoading({
              title: '正在核验工商信息',
              mask:true
            })
            // 查询公司是否认证过
            requestClient.getAuthCompany(that.data.formData.name,function(res){
              if(res.data.data){
                wx.hideLoading({
                  success: (res) => {},
                })
                wx.showModal({
                  title:'变更失败',
                  content:'当前企业已被创建！',
                  confirmText:'查看详情',
                  success(r){
                    if(r.confirm){
                      wx.navigateTo({
                        url: '../scancompany/index?companyName=' + that.data.formData.name,
                      })
                    } else {

                    }
                  }
                })
              } else {
                // 认证公司
                requestClient.authCompany(that.data.formData.name,function(resCompany){
                  if(resCompany.data.data){
                    requestClient.changeCompany(that.data.companyId,that.data.formData.name,function(res){
                      if(res.data.data){
                        wx.hideLoading({
                          success: (res) => {},
                        })
                        wx.showModal({
                          title:'变更企业名称',
                          content:'变更成功',
                          showCancel:false,
                          success(r){
                            wx.navigateBack({
                              delta: 1,
                            })
                          }
                        })
                      } else {
                        wx.showToast({
                          title: '变更企业名称失败',
                          icon:'error'
                        })
                      }
                    });
                  } else {
                    wx.hideLoading({
                      success: (res) => {},
                    })
                    wx.showModal({
                      title:'工商核验失败',
                      content:'暂未查询到该企业的工商信息，请确认企业全称！',
                      showCancel:false,
                      success(r){
                        
                      }
                    })
                  }
                })
              }
            })
        }
    })
  }
})
